let app = getApp();
Page({
  onLoad(){
  },
  onShow(){
    let token = wx.getStorageSync('token');
    if (token === "") { 
      this.setData({ login: false, index: this.data.index + 1 });
      if (this.data.index == 1) {
        wx.navigateTo({ url: '/pages/login/login' })
      }
      return false
    }
    this.setData({ month: new Date().getMonth() + 1, year: new Date().getFullYear(), login: true});
    this.xingqi();
    this.jingxuan();
    this.headinfo();
  },
  onReachBottom() {
    this.jingxuan();
  },
  data:{
    kong:[],
    days:['日','一','二','三','四','五','六'],
    lists:[],
    look:false,
    index:0
  },
  headinfo(){
    let json={user_id:wx.getStorageSync('userdata').user_id}
    app.request('book/community/baby_read_statistic', 'POST', json,'headinfob',this)
  },
  headinfob(data){
    this.setData({headinfo:data.data})
  },
  xingqi(){    
    //首日对应星期
    let a = new Date();
    let a_year = this.data.year;
    let a_yue = this.data.month;
    if(parseInt(a_yue)<10){
      a_yue = "0"+a_yue
    }
    let b = new Date(a_year + '-' + a_yue+'-01');// y-m-d m=当前月数
    let b_xingqi=b.getDay();
    let array=new Array();
    for (let i = 0; i < b_xingqi; i++) {
      array.push(i);
    } 
    this.setData({kong:array});
    //当前月份天数
    let c = new Date(a_year, a_yue, 0).getDate();// y,m,d m-1=实际月数
    let array2 = new Array();
    for(let i=0;i<c;i++){
      array2.push({had:"1"})
    }
    this.setData({dates:array2});
    let json = {
      token:wx.getStorageSync('token'),
      user_id: wx.getStorageSync('userdata') ? wx.getStorageSync('userdata').user_id:"",
      date:this.data.year+'-'+this.data.month
    }
    app.request('book/community/baby_sign_list','POST',json,'infob',this);
    if (this.data.month != new Date().getMonth()+1){
      return false;
    }
    let params = new Object();
    let json2 = { had: "3" }
    let date = new Date().getDate();
    params['dates[' + (date - 1) + ']'] = json2;
    this.setData(params);
  },
  infob(data){
    if (data.code != 200) {
      app.tishi("提示", data.msg);
      return false
    }
    let dates=this.data.dates;
    let _this = this;
    data.data.forEach((item)=>{
        let params = new Object();
        let json = { had:"2",id:item.id,day:item.day}     
        if (parseInt(item.day)===new Date().getDate()){//今天
          json.today = true
        }
        params['dates[' + (parseInt(item.day) - 1) + ']'] = json;
        _this.setData(params)
    })
  },
  reducemonth() {//前月份
    if (this.data.month <= 1) {
      this.setData({ month: 12, year: this.data.year - 1 });
    } else {
      this.setData({ month: this.data.month - 1 });
    }
    this.xingqi();
  },
  addmonth() {//后月份
    if (this.data.month >= 12) {
      this.setData({ month: 1, year: this.data.year + 1 });
    } else {
      this.setData({ month: this.data.month + 1 });
    }
    this.xingqi();
  },
  detail(e){
    let plan_id = e.currentTarget.id;
    this.setData({ day: e.currentTarget.dataset.day});
    let json={
      token: wx.getStorageSync('token'),
      user_id: wx.getStorageSync('userdata').user_id,
      plan_id: plan_id
    }
    app.request('book/community/baby_sign_detail', 'POST', json,'detailb',this)
  },
  detailb(data){
    if (data.code != 200) {
      app.tishi("提示", data.msg);
      return false
    }
    data.data.sign_info.forEach((item)=>{
      item.img = item.img.split('@')[0];
    })
    this.setData({ look: true, dayimgs: data.data.sign_info, remark: data.data.remark});
  },
  del(e){
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '删除今日打卡记录？',
      success(res){
        if(res.confirm){
          let plan_id = e.currentTarget.id;
          let date = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate();
          let json = {
            token: wx.getStorageSync('token'),
            user_id: wx.getStorageSync('userdata').user_id,
            plan_id: plan_id,
            date: date
          }
          app.request('book/community/baby_sign_delete', 'POST', json, 'delb', _this)
        }
      }
    })    
  },
  delb(data){
    if(data.code==200){
      wx.showToast({title: data.msg,duration:1000})
      this.onShow();
    }
  },
  quxiao(){
    this.setData({ look:false})
  },
  daka(){
    wx.navigateTo({url: '/pages/find/xuanze/xuanze'})
    //wx.navigateTo({url:"/pages/babyhouse/babyhouse?from=find"})
  },
  babyhouse(){
    wx.navigateTo({ url: "/pages/babyhouse/babyhouse?from=my" })
  },
  jingxuan(){
    let user_id = wx.getStorageSync('userdata').user_id || "";
    app.request('book/community/recommend_book_list','POST',{user_id:user_id},'jingxuanb',this)
  },
  jingxuanb(data){
    if(data.code!=200){
      app.tishi("提示",data.msg);
      return false
    }
    let lists = this.data.lists;
    data.data.forEach((item)=>{
      item.img_medium = item.img_medium.split('@')[0];
    })
    lists = lists.concat(data.data)
    this.setData({ lists: lists })
  },
  getdetail(e) {//去绘本详情页
    if (app.repeat(e.timeStamp) == false) { return; }
    let bookid = e.currentTarget.id;
    if (bookid!=0){
      wx.navigateTo({ url: '../detail/detail?bookid=' + bookid });
      return false
    }
    let src = e.currentTarget.dataset.src;
    let arr = [];
    for (let i = 0; i < this.data.dayimgs.length;i++){
      arr.push(this.data.dayimgs[i].img);
    }
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: arr// 需要预览的图片http链接列表
    })
  },
  login(e) {
    if (app.repeat(e.timeStamp) == false) { return; }
    wx.navigateTo({ url: '/pages/login/login' })
  },
  buzhou(){
    wx.navigateTo({url: '/pages/find/buzhou/buzhou',})
  }
})